package com.gaofei.controller;

import java.io.File;
import java.util.Arrays;
import java.util.UUID;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.gaofei.domain.Sku;
import com.gaofei.domain.SkuVo;
import com.gaofei.domain.Spec;
import com.gaofei.domain.Spu;
import com.gaofei.service.SkuService;
import com.gaofei.service.SpecService;
import com.gaofei.service.SpuService;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("sku")
public class SkuController {

	@Reference
	SkuService skuService;
	
	@Reference
	SpuService spuService;
	
	@Reference
	SpecService specService;
	
	@Value("${upload.path}")
	private String fileUploadPath;
	
	
	@RequestMapping("update")
	@ResponseBody
	public boolean update(Sku sku,Model m,@RequestParam("specId")Integer [] specIds,@RequestParam("optionId")Integer[] optionIds,MultipartFile cartThumbnailFile,MultipartFile imageFile) {
		try {
			String cartThumbnailFilePath = fileUpload(cartThumbnailFile);
			String imageFilePath = fileUpload(imageFile);
			//1.添加sku,并且记住返回sku的id用来保存到商品规格中间表,//2.添加中间表
			if(cartThumbnailFilePath!=null&&cartThumbnailFilePath!="") {
				sku.setCartThumbnail(cartThumbnailFilePath);
			}
			if(imageFilePath!=null&&imageFilePath!="") {
				sku.setImage(imageFilePath);
			}
			skuService.update(sku,specIds,optionIds);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
		
	}
	
	//跳转到修改页面
	@RequestMapping("toUpdate")
	public String toUpdate(Integer skuId,Model m) {
		//根据skuId查询sku
		Sku sku = skuService.findSkuBySkuID(skuId);
		m.addAttribute("sku", sku);
		
		PageInfo<Spec> specInfo = specService.list(1, Integer.MAX_VALUE);
		m.addAttribute("specList", specInfo.getList());
		return "sku/update";
	}
	
	
	
//	文件上传的方法
	public String fileUpload(MultipartFile picFileName) {
		String filename = "";
		if(picFileName!=null && !picFileName.isEmpty()) {
			String originalFilename = picFileName.getOriginalFilename();
			filename =UUID.randomUUID()+originalFilename.substring(originalFilename.lastIndexOf("."));
			try {
				picFileName.transferTo(new File(fileUploadPath, filename));
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
		return filename;
	}
	
	
	
	@RequestMapping("add")
	@ResponseBody
	public boolean add(Sku sku,Model m,@RequestParam("specId")Integer [] specIds,@RequestParam("optionId")Integer[] optionIds,MultipartFile cartThumbnailFile,MultipartFile imageFile) {
		try {
			String cartThumbnailFilePath = fileUpload(cartThumbnailFile);
			String imageFilePath = fileUpload(imageFile);
			//1.添加sku,并且记住返回sku的id用来保存到商品规格中间表,//2.添加中间表
			sku.setImage(imageFilePath);
			sku.setCartThumbnail(cartThumbnailFilePath);
			skuService.addSku(sku,specIds,optionIds);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
		
	}

	// 批量删除
	@RequestMapping("toAdd/{spuId}/{num}")
	public String toAdd(@PathVariable("spuId")Integer  spuId,@PathVariable("num") int num,Model m) {
		System.out.println(spuId+"========="+num);
		//根据spuId查询spu
		Spu spu = spuService.findSpuById(spuId);
		m.addAttribute("spu", spu);
		//为了再下拉框中显示规格名称,查询规格
		PageInfo<Spec> info = specService.list(1, Integer.MAX_VALUE);
		m.addAttribute("specList", info.getList());
		return "sku/add";
	}

	// 批量删除
	@RequestMapping("delBatch")
	@ResponseBody
	public boolean delBatch(String ids) {
		return skuService.delBatch(ids) > 0;
	}

	@RequestMapping("list")
	public String list(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize,
			Model m, SkuVo vo) {

		PageInfo<Sku> list = skuService.list(pageNum, pageSize, vo);
		m.addAttribute("info", list);
		m.addAttribute("skuVo", vo);

		return "sku/list";
	}

}
